<?php
namespace app\controllers;

use app\models\CateRecorder;
use app\models\GoodsRecorder;

class GoodsController extends BaseController{
    public function actionAdd(){
        $post = \Yii::$app->request->post();
        $id = intval($post['GoodsRecorder']['id']);
        if($id){
            $model = GoodsRecorder::find()->where('id=:id',[':id' => $id])->one();
            if(!$model){
                $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
                return json_encode($result);
            }
        }else{
            $model = new GoodsRecorder();
            $model->addtime = time();
        }
        $result = $model->add($post);
        return json_encode($result);
    }

    public function actionList(){
        $post = \Yii::$app->request->post('page');
        $page = intval($post['page'])-1;
        $pagesize = intval($post['pagesize']);
        $offset = $page*$pagesize;
        $list = GoodsRecorder::find();
        $count = $list->count();
        $list = $list->limit($pagesize)->offset($offset)->orderBy('addtime DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $v['goods_pics'] = json_decode($v['goods_pics'],true);
            $arr=[];
            foreach($v['goods_pics'] as $pv){
                $arr[]=str_replace('../',\Yii::$app->params['pic_url'],$pv);
            }
            $list[$k]['goods_pics'] = $arr;
            $list[$k]['thumb_pic'] = empty($v['thumb_pic'])?'':str_replace('../',\Yii::$app->params['pic_url'],$v['thumb_pic']);
            $v['goods_type'] = json_decode($v['goods_type'],true);
            $arr=[];
            foreach($v['goods_type'] as $t){
                $arr[]=\Yii::$app->params['goods_type'][$t];
            }
            $list[$k]['goods_type'] = implode(",",$arr);
            $cate = CateRecorder::find()->where('id='.$v['goods_cate'])->one();
            $list[$k]['cate_name'] = $cate->cate_name;
        }
        $result = ['result'=>1,'data'=>$list,'count'=>$count];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

    public function actionOne(){
        $id = \Yii::$app->request->post('id');
        $model = GoodsRecorder::find()->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model['goods_type'] = json_decode($model['goods_type'],true);
        $model['goods_pics'] = json_decode($model['goods_pics'],true);
        $arr=[];
        foreach($model['goods_pics'] as $pv){
            $arr[]=str_replace('../',\Yii::$app->params['pic_url'],$pv);
        }
        $model['goods_pics'] = $arr;
        $model['thumb_pic'] = str_replace("../",\Yii::$app->params['pic_url'],$model['thumb_pic']);
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }

    public function actionDel(){
        $id = \Yii::$app->request->post('id');
        $model = GoodsRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->delete();
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


}